import Ability, { FightStatusHolder, Fighter } from "../ability.js";
import Member from "./member.js";



export default class FightM extends Member {
	host: FightStatusHolder;

	constructor(host: FightStatusHolder) {
		super(host);
	}


	/**总生命值 */
	get maxHp(): number {
		return this.host.maxHp;
	}

	/**当前生命值 */
	get currentHp(): number {
		return this.host.currentHp;
	}
	set currentHp(hp: number) {
		this.host.currentHp = hp;
	}

	/**攻击力 */
	get attackPower(): number {
		return this.host.attackPower;
	}

	/**防御力 */
	get defensivePower(): number {
		return this.host.defensivePower;
	}

	/**敏捷度 */
	get agility(): number {
		return this.host.agility;
	}

	/**
	 * 获取本次伤害值，不考虑闪避
	 */
	damageTo(target: Fighter): number {
		let damage = this.attackPower - target.defensivePower;
		if (damage < 0) damage = 0;
		damage++;
		return damage;
	}

	/**
	 * 闪避成功概率
	 * @param attacker 攻击者
	 * @returns  闪避成功概率，取值为0-1
	 */
	dodgeProbability(attacker: FightStatusHolder): number {
		if (this.agility > attacker.agility) {
			return 0.5 + 0.5 * ((this.agility - attacker.agility) / (this.agility == 0 ? 1 : this.agility));
		} else {
			return 0.5 - 0.5 * ((attacker.agility - this.agility) / (attacker.agility == 0 ? 1 : attacker.agility));
		}
	}

	/**
	 * 是否闪避成功
	 * @param attacker 攻击者
	 */
	dodgeSuccess(attacker: FightStatusHolder): boolean {
		let dp = this.dodgeProbability(attacker);
		// console.log(`闪避成功概率为：${dp}`);
		return Math.random() < dp;
	}

	/**攻击，考虑闪避 */
	hit(target: Fighter) {
		let status = {
			damage: 0,
			success: false,
			dodgeSuccess: true
		};

		//如果对方闪避成功
		if (target.dodgeSuccess(this.host)) return status;


		let hurtResult = this.hurt(target);
		status = {
			damage: hurtResult.damage,
			success: hurtResult.success,
			dodgeSuccess: false
		};
		return status;
	}

	/**
	 * 对target造成伤害，不考虑闪避
	 */
	hurt(target: Fighter) {
		let damage = this.damageTo(target);//受到的伤害
		target.currentHp -= damage;
		/**如果生命值小于0 */
		if (this.currentHp <= 0) {
			this.currentHp = 0;
		}

		return {
			damage: damage,
			criticalStrike: false,
			success: true
		};
	}

	isAlive(): boolean {
		return this.currentHp > 0;
	}

	/**复活 */
	reborn() {
		this.currentHp = this.maxHp;
	}

	/**自杀 */
	suicide(): void {
		this.currentHp = 0;
	}
}


